package com.ruoyi.dormitory.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dormitory.domain.Dormitory;
import com.ruoyi.dormitory.mapper.DormitoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dormitory.mapper.DormitoryRecordMapper;
import com.ruoyi.dormitory.domain.DormitoryRecord;
import com.ruoyi.dormitory.service.IDormitoryRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 宿舍住宿记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-26
 */
@Service
public class DormitoryRecordServiceImpl implements IDormitoryRecordService {
    @Autowired
    private DormitoryMapper dormitoryMapper;


    @Autowired
    private DormitoryRecordMapper dormitoryRecordMapper;

    /**
     * 查询宿舍住宿记录
     *
     * @param
     * @return 宿舍住宿记录
     */
    @Override
    public DormitoryRecord selectDormitoryRecordByDormitoryNumber(Integer recordNumber) {
        return dormitoryRecordMapper.selectById(recordNumber);
//        return dormitoryRecordMapper.selectDormitoryRecordByDormitoryNumber(recordNumber);
    }

    /**
     * 查询宿舍住宿记录列表
     *
     * @param dormitoryRecord 宿舍住宿记录
     * @return 宿舍住宿记录
     */
    @Override
    public List<DormitoryRecord> selectDormitoryRecordList(DormitoryRecord dormitoryRecord) {
        return dormitoryRecordMapper.selectDormitoryRecordList(dormitoryRecord);
    }

    /**
     * 新增宿舍住宿记录
     *
     * @param dormitoryRecord 宿舍住宿记录
     * @return 结果
     */
    @Override
    public int insertDormitoryRecord(DormitoryRecord dormitoryRecord) {
        Dormitory dormitory = dormitoryMapper.selectById(dormitoryRecord.getDormitoryNumber());
        if (dormitory == null) {
            throw new UtilException("请输入正确的宿舍编号");
        }
        String pattern = "^(0\\d{5}$)";
        if (!dormitoryRecord.getEmployeeNumber().matches(pattern)) {
            throw new UtilException("请输入正确的员工编号");
        }
        String phonePattern = "^1[3-9]\\d{9}$";
        if (!(dormitoryRecord.getPhoneNumber().toString().matches(phonePattern))) {
            throw new UtilException("请输入正确的手机号码");
        }
        return dormitoryRecordMapper.insert(dormitoryRecord);
    }

    /**
     * 修改宿舍住宿记录
     *
     * @param dormitoryRecord 宿舍住宿记录
     * @return 结果
     */
    @Override
    public int updateDormitoryRecord(DormitoryRecord dormitoryRecord) {
        Dormitory dormitory = dormitoryMapper.selectById(dormitoryRecord.getDormitoryNumber());
        if (dormitory == null) {
            throw new UtilException("请输入正确的宿舍编号");
        }
//        String pattern = "^(0\\d{5}$)";
//        if (!dormitoryRecord.getEmployeeNumber().matches(pattern)) {
//            throw new UtilException("请输入正确的员工编号");
//        }
        String phonePattern = "^1[3-9]\\d{9}$";
        if (!(dormitoryRecord.getPhoneNumber().toString().matches(phonePattern))) {
            throw new UtilException("请输入正确的手机号码");
        }
        return dormitoryRecordMapper.updateById(dormitoryRecord);
    }

    /**
     * 批量删除宿舍住宿记录
     *
     * @param
     */
    @Override
    public int deleteDormitoryRecordByDormitoryNumbers(String recordNumbers) {
        return dormitoryRecordMapper.deleteDormitoryRecordByDormitoryNumbers(Convert.toStrArray(recordNumbers));
    }

    /**
     * 删除宿舍住宿记录信息
     *
     * @param
     * @return 结果
     */
    @Override
    public int deleteDormitoryRecordByDormitoryNumber(Integer recordNumber) {
        return dormitoryRecordMapper.deleteDormitoryRecordByDormitoryNumber(recordNumber);
    }

    @Override
    public List<DormitoryRecord> selectDormitoryRecordListById(String dormitoryId) {
        return dormitoryRecordMapper.selectList(new LambdaQueryWrapper<DormitoryRecord>().eq(DormitoryRecord::getDormitoryNumber, dormitoryId));
    }
}
